// src/router/index.js
import {
    // 创建路由的api
    createRouter,
    // 创建路由的历史模式
    createWebHashHistory
} from 'vue-router'
import { ElLoading } from 'element-plus';

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            redirect: '/index',
            path: "/"
        },
        {
            name: "index",
            path: "/home",
            component: () => import("@/view/home/index.vue"),
            children: [
                {
                    name: "index",
                    path: "/index",
                    menuName: "首页",
                    menuIcon: "HomeFilled",
                    component: () => import("@/view/home/components/index/index.vue")
                },
                {
                    name: "merchantApproval",
                    path: "/merchantApproval",
                    menuName: "商家审批管理",
                    menuIcon: "Check",
                    component: () => import("@/view/home/components/merchantApproval/merchantApproval.vue")
                },
                {
                    name: "user",
                    path: "/user",
                    menuName: "用户管理",
                    menuIcon: "User",
                    component: () => import("@/view/home/components/user/user.vue")
                },
                {
                    name: "shop",
                    path: "/shop",
                    menuName: "商家管理",
                    menuIcon: "Shop",
                    component: () => import("@/view/home/components/shop/shop.vue")
                },
                {
                    name: "category",
                    path: "/category",
                    menuName: "分类管理",
                    menuIcon: "Collection",
                    component: () => import("@/view/home/components/category/category.vue")
                },
                {
                    name: "goods",
                    path: "/goods",
                    menuName: "商品管理",
                    menuIcon: "Goods",
                    component: () => import("@/view/home/components/goods/goods.vue")
                },
                {
                    name: "order",
                    path: "/order",
                    menuName: "订单管理",
                    menuIcon: "Document",
                    component: () => import("@/view/home/components/order/order.vue")
                },
                // {
                //     name: "recharge",
                //     path: "/recharge",
                //     menuName: "充值记录",
                //     menuIcon: "Coin",
                //     component: () => import("@/view/home/components/recharge/recharge.vue")
                // },
            ]
        },
        {
            name: "login",
            path: "/login",
            component: () => import("@/view/login/index.vue")
        },
        {
            // 匹配所有未定义的路由
            path: '/:pathMatch(.*)*',
            name: 'NotFound',
            component: () => import('@/view/404/index.vue') // 这里假设 404 页面组件路径
        }
    ]
})

let loadingInstance = null;

// 路由守卫
router.beforeEach((to, from, next) => {
    loadingInstance = ElLoading.service({
        lock: true,
        text: '页面加载中，请稍后...',
        background: '#fff'
    });

    // 检查是否已登录（存在adminToken）
    const adminToken = localStorage.getItem('adminToken');

    // 如果访问的不是登录页面且没有token，则跳转到登录页
    if (to.path !== '/login' && !adminToken) {
        next('/login');
    } else {
        next();
    }
});

router.afterEach(() => {
    if (loadingInstance) {
        loadingInstance.close();
    }
});

export default router